<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/3/30
 * Time: 9:07
 */

namespace app\mobile\controller;


class Chuzu extends Base
{
    public function _initialize(){
        parent::_initialize();
        $id = input('param.id/d',0);

        $this->assign('id',$id);
    }
    //楼盘列表页
    public function index(){
        $this->search();
        $this->getAttr();
        $this->getLists();
        $this->assign('price',$this->getPrice());
        $this->assign('mianji',$this->getMianji());
        $this->assign('room',$this->getRoom());
        $this->assign('zhuangxiu',cache('linkmenu_9')['cate']);
        $this->assign('rentalpay',cache('linkmenu_15')['cate']);
        $this->assign('chaoxiang',cache('linkmenu_12')['cate']);
        $this->assign('title','出租房');
        $this->assign('ajaxdata',true);
        return $this->fetch();
    }
    //楼盘详细页
    public function detail(){
        $id   = input('param.id/d',0);
        $info = model('rental')->where(['status'=>1,'id'=>$id])->find();
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $estate  = model('estate_data')->where(['id'=>$info['estate_id']])->value('info');
        $this->setSeo($info);
        model('rental')->where(['id'=>$id])->setInc('hits');
        if($info['user_id']){
            $this->getUserinfo($info['user_id']);
        }
        $this->assign('zhuangxiu',$this->getAttrName(9,$info['zhuangxiu']));
        $this->assign('chaoxiang',$this->getAttrName(12,$info['chaoxiang']));
        $this->assign('peitao',$this->getAttrName(13,$info['peitao']));
        $this->assign('rentalpay',$this->getAttrName(15,$info['rental_pay']));
        $this->assign('rentaltype',$this->getAttrName(14,$info['rental_type']));
        $this->assign('info',$info);
        $this->assign('estate',$estate);
        return $this->fetch();
    }
    private function getUserinfo($id){
        $info = model('user')->where(['id'=>$id])->field('id,nickname,headimgurl,buy_phone,company,level')->find();
        $this->assign('user',$info);
    }
   public function getAjaxLists(){
        $page  = input('get.page/d',2);
        $return['status'] = 0;
        $where = $this->search();
        $sort  = $this->order();
        $field = 'id,title,img,address,price,room,ting,wei,mianji,floor,total_floor,niandai,contact,contact_phone';
        $lists = model('rental')->where($where)->field($field)->order($sort)->limit(10)->page($page)->select();
        if($lists){
            $lists = objToArray($lists);
            $return['status'] = 1;
            foreach($lists as &$v){
                $v['url'] = url('Chuzu/detail',['id'=>$v['id']]);
            }
            $return['data'] = $lists;
        }
        return json($return);
    }
    private function getLists(){
        $pagesize = 10;
        $obj   = model('rental');
        $where = $this->search();
        $sort  = $this->order();
        $field = 'id,title,estate_id,estate_name,zhuangxiu,img,address,price,yajin,rental_pay,user_type,room,file,ting,wei,chaoxiang,user_id,mianji,floor,total_floor,niandai,contact,contact_phone';
        $lists = $obj->where($where)->field($field)->order($sort)->limit($pagesize)->select();
        $total = $obj->where($where)->count();
        $total_page = ceil($total/$pagesize);
        $this->assign('totalpage',$total_page);
        $this->assign('lists',$lists);
    }
    private function search(){
        $param['area'] = input('param.area',0);
        $param['price']      = input('param.price',0);
        $param['mianji']       = input('param.mianji',0);
        $param['room']       = input('param.room',0);//户型
       // $param['chao']       = input('param.chao',0);//楼盘朝向
        $param['sort']       = input('param.sort/d',0);//排序
        $param['usertype']  = input('param.usertype/d',0);//1个人房源2中介房源
       // $param['rental_type'] = input('param.rentaltype/d',0);//出租方式整租 合租
        $data['status']      = 1;
        $data['city']        = $this->cityInfo['id'];
        $keyword = input('get.keyword');
        if($keyword){
            $param['keyword'] = $keyword;
            $data['title'] = ['like','%'.$keyword.'%'];
        }
        if(!empty($param['area'])){
            $data['area'] = $param['area'];
        }
        if(!empty($param['price'])){
            $data['price'] = $this->getPrice($param['price']);
        }
        if(!empty($param['mianji'])){
            $data['mianji'] = $this->getMianji($param['mianji']);
        }
        if(!empty($param['room'])){
            $data['room'] = $this->getRoom($param['room']);
        }
        if(!empty($param['usertype'])){
            $data['user_type'] = $param['usertype'];
        }
        $this->assign('param',$param);
        $this->assign('search',$param);
        return $data;
    }
    private function getAttr($return=''){
        $chaoxiang     = cache('linkmenu_12')['cate'];//朝向
        $retaltype     = cache('linkmenu_14')['cate'];//出租方式
        if($return){
            $data = array_merge($chaoxiang,$retaltype);
            $temp = [];
            foreach($data as $v){
                $temp[$v['id']] = $v;
            }
            return $temp;
        }else{
            $this->assign('chaoxiang',$chaoxiang);
        }
    }
    private function getAttrName($cate_id,$id){
        $cate = cache('linkmenu_'.$cate_id);
        $cate = $cate['cate'];
        if(is_numeric($id)){
            return $cate[$id]['name'];
        }
        $id_arr = explode(',',$id);
        $temp = [];
        if($id){
            if($id_arr){
                foreach($id_arr as $v){
                    $temp[] = '<li>'.$cate[$v]['name'].'</li>';
                }
            }
        }
        return implode('',$temp);
    }

    public function getPrice($key=''){
        $data = [
            1 => ['name'=>'500元以下','value'=>[0,500]],
            2 => ['name'=>'500-1000元/月','value'=>[500,1000]],
            3 => ['name'=>'1000-1500元/月','value'=>[1000,1500]],
            4 => ['name'=>'1500-2000元/月','value'=>[1500,2000]],
            5 => ['name'=>'2000-2500元/月','value'=>[2000,2500]],
            6 => ['name'=>'2500-3000元/月','value'=>[2500,3000]],
            7 => ['name'=>'3000元以上','value'=>'3000']
        ];
        if($key){
            if($key == 7){
                $str = ['egt',$data[$key]['value']];
            }else{
                $str = ['between',$data[$key]['value']];
            }
        }else{
            $str = $data;
        }
        return $str;
    }
    //面积
    public function getMianji($key=''){
        $data = [
            1 => ['name'=>'50平米以下','value'=>[0,50]],
            2 => ['name'=>'50-70平米','value'=>[50,70]],
            3 => ['name'=>'70-90平米','value'=>[70,90]],
            4 => ['name'=>'90-110平米','value'=>[90,110]],
            5 => ['name'=>'110-130平米','value'=>[110,130]],
            6 => ['name'=>'130-150平米','value'=>[130,150]],
            7 => ['name'=>'150平米以上','value'=>'150']
        ];
        if($key){
            if($key == 7){
                $str = ['egt',$data[$key]['value']];
            }else{
                $str = ['between',$data[$key]['value']];
            }
        }else{
            $str = $data;
        }
        return $str;
    }
    //户型
    private function getRoom($key=0){
        $data = [
            1 => '一室',
            2 => '二室',
            3 => '三室',
            4 => '四室',
            5 => '五室以上'
        ];
        if ($key) {
            if ($key == 5) {
                $str = ['egt', $key];
            } else {
                $str = $key;
            }
        } else {
            $str = $data;
        }
        return $str;
    }
    //排序
    private function order(){
        $sort = input('param.sort/d',0);
        switch($sort){
            case 1:
                $order = 'price asc';
                break;
            case 2:
                $order = 'price desc';
                break;
            default:
                $order = 'ordid asc,id desc';
                break;
        }
        $this->assign('sort',$sort);
        return $order;
    }

}